/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.cxhd.pacs.tools;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import net.lingala.zip4j.core.ZipFile;
import net.lingala.zip4j.exception.ZipException;
import net.lingala.zip4j.model.FileHeader;
import net.lingala.zip4j.model.ZipParameters;
import net.lingala.zip4j.util.Zip4jConstants;

/**
 *
 * @author Administrator
 */
public class ZipFileTool {

    public static boolean upZipFile(List<File> files, String path, String password, boolean delete, String movePath) {
        if (files.isEmpty()) {
            return false;
        }
        try {
            ZipFile zip = new ZipFile(path);
            ZipParameters parameters = new ZipParameters();
            parameters.setCompressionMethod(Zip4jConstants.COMP_DEFLATE); // 压缩方式
            parameters.setCompressionLevel(Zip4jConstants.DEFLATE_LEVEL_NORMAL); // 压缩级别
            parameters.setEncryptFiles(true);
            parameters.setEncryptionMethod(Zip4jConstants.ENC_METHOD_STANDARD); // 加密方式
            parameters.setPassword(password);
            zip.addFiles((ArrayList) files, parameters);
            if (zip.isValidZipFile()) {
                if (delete) {
                    for (File file : files) {
                        file.delete();

                    }
                }
                return true;
            }else{
              new File(path).delete();
            }
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return false;
    }

    public static boolean upZipFile(File file, String path, String password, boolean delete, String movePath) {

        try {
            ZipFile zip = new ZipFile(path);
            ZipParameters parameters = new ZipParameters();
            parameters.setCompressionMethod(Zip4jConstants.COMP_DEFLATE); // 压缩方式
            parameters.setCompressionLevel(Zip4jConstants.DEFLATE_LEVEL_NORMAL); // 压缩级别
            parameters.setEncryptFiles(true);
            parameters.setEncryptionMethod(Zip4jConstants.ENC_METHOD_STANDARD); // 加密方式
            parameters.setPassword(password);
            zip.addFile(file, parameters);
            if (zip.isValidZipFile()) {
                if (delete) {

                    file.delete();

                }
                return true;
            }
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return false;
    }

    public static File[] UnZip(String path, String password) {

        try {
            System.out.println(path);
            ZipFile zip = new ZipFile(path);
           // if (zip.isValidZipFile()) {
              
                    System.out.println("解压文件");
                    String content = path.substring(0, path.lastIndexOf("\\") + 1);
                    
                    //  content = content.substring(0, content.length() - 4);
                    // String password = ApolloUtil.makePSWD(content.replace("_", ""));
                    if(password!=null){
                    zip.setPassword(password);
                    }
                    File destDir = new File(content);
                    if (destDir.isDirectory() && !destDir.exists()) {
                        destDir.mkdir();
                    }
                    zip.extractAll(content);

                    List<FileHeader> headerList = zip.getFileHeaders();
                    List<File> extractedFileList = new ArrayList<File>();
                    for (FileHeader fileHeader : headerList) {
                        if (!fileHeader.isDirectory()) {
                            extractedFileList.add(new File(destDir, fileHeader.getFileName()));
                        }
                    }
                    File[] extractedFiles = new File[extractedFileList.size()];
                    extractedFileList.toArray(extractedFiles);
                    return extractedFiles;
               
//            }else{
//                System.out.println("压缩包损坏1111");
//            }
           // return null;
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return null;
    }

}
